/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package UI;

import BL.BLOrden;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author Daniel.Cerdas
 */
public class FormViaticos extends javax.swing.JFrame {

    
    /**
     * Creates new form FormViaticos
     */
    FrmOrdenDeViaticos VentanaViaticos ;
    FrmConceptos VentanaConceptos;
    public FormViaticos() {
        initComponents();
    }
    
        
    public boolean soloNumeros(String hilera)
    {
      
        
        for(int i=0; i<hilera.length();i++){
        if(Character.isLetter(hilera.charAt(i)))
                {
                   return false;
                }
        }
        
        return true;
    }
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenu1 = new javax.swing.JMenu();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu2 = new javax.swing.JMenu();
        jMenu3 = new javax.swing.JMenu();
        jMenu4 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem6 = new javax.swing.JMenuItem();
        BtnLiquidacion = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jMenuBar2 = new javax.swing.JMenuBar();
        jMenu5 = new javax.swing.JMenu();
        jMenuItem5 = new javax.swing.JMenuItem();
        mnNuevoViatico = new javax.swing.JMenuItem();
        menuAnular = new javax.swing.JMenuItem();
        jMenuItem4 = new javax.swing.JMenuItem();
        MnuConceptos = new javax.swing.JMenu();
        jMenuItem3 = new javax.swing.JMenuItem();
        jmUsuarios = new javax.swing.JMenuItem();

        jMenu1.setText("jMenu1");

        jMenu2.setText("File");
        jMenuBar1.add(jMenu2);

        jMenu3.setText("Edit");
        jMenuBar1.add(jMenu3);

        jMenu4.setText("jMenu4");

        jMenuItem1.setText("jMenuItem1");

        jMenuItem2.setText("jMenuItem2");

        jMenuItem6.setText("jMenuItem6");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        BtnLiquidacion.setText("Liquidacion");
        BtnLiquidacion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnLiquidacionActionPerformed(evt);
            }
        });

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/fod-logo.png"))); // NOI18N

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(153, 153, 153));
        jLabel2.setText("Omar Dengo");

        jMenu5.setText("Inicio");

        jMenuItem5.setText("Imprimir");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        jMenu5.add(jMenuItem5);

        mnNuevoViatico.setText("Nuevo Viático");
        mnNuevoViatico.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnNuevoViaticoActionPerformed(evt);
            }
        });
        jMenu5.add(mnNuevoViatico);

        menuAnular.setText("Anular Viático");
        menuAnular.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuAnularActionPerformed(evt);
            }
        });
        jMenu5.add(menuAnular);

        jMenuItem4.setText("Reporte General");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu5.add(jMenuItem4);

        jMenuBar2.add(jMenu5);

        MnuConceptos.setText("Mantenimiento");

        jMenuItem3.setText("Conceptos");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        MnuConceptos.add(jMenuItem3);

        jmUsuarios.setText("Usuarios");
        jmUsuarios.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmUsuariosActionPerformed(evt);
            }
        });
        MnuConceptos.add(jmUsuarios);

        jMenuBar2.add(MnuConceptos);

        setJMenuBar(jMenuBar2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(70, 70, 70)
                .addComponent(BtnLiquidacion, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(484, 484, 484))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(115, 115, 115))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(65, 65, 65))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addGap(187, 187, 187)
                .addComponent(BtnLiquidacion))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void mnNuevoViaticoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnNuevoViaticoActionPerformed
     
            String hilera= JOptionPane.showInputDialog( null, "Ingrese el consecutivo de la orden");
            
            while(!this.soloNumeros(hilera)){
                JOptionPane.showMessageDialog(null, "El consecutivo de la orden solo debe de contener numeros");
                hilera= JOptionPane.showInputDialog( null, "Ingrese el consecutivo de la nueva orden");
            }
            
            
            int numOrden= Integer.parseInt(hilera);
        try {
            if(BLOrden.existeOrden(numOrden)){
                JOptionPane.showMessageDialog(null, "El consecutivo de la orden ya existe");
            }
            else{
                
                FrmOrdenDeViaticos v= new FrmOrdenDeViaticos(numOrden);
                v.setVisible(true);
                
                
               
            }
        } catch (SQLException | ClassNotFoundException ex) {
            Logger.getLogger(FormViaticos.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_mnNuevoViaticoActionPerformed

    private void menuAnularActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuAnularActionPerformed
        FrmAnular v= new FrmAnular();
        v.setVisible(true);
    }//GEN-LAST:event_menuAnularActionPerformed


    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
        FrmReporte v= new FrmReporte();
        v.setVisible(true);
    }//GEN-LAST:event_jMenuItem4ActionPerformed



    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        
// TODO add your handling code here:
        VentanaConceptos = new FrmConceptos();
        VentanaConceptos.setVisible(true);
        
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem5ActionPerformed
        try {
            JasperReport jr= null;
            String archivo= "C:\\Users\\David\\Documents\\NetBeansProjects\\ControlViaticos\\src\\UI\\Reports\\impresion.jasper";
            String hilera= JOptionPane.showInputDialog( null, "Ingrese el consecutivo de la orden");
            
            while(!this.soloNumeros(hilera)){
                JOptionPane.showMessageDialog(null, "El consecutivo de la orden solo debe de contener numeros");
                hilera= JOptionPane.showInputDialog( null, "Ingrese el consecutivo de la nueva orden");
            }
            
            
            int numOrden= Integer.parseInt(hilera);
        
            if(!BLOrden.existeOrden(numOrden)){
                JOptionPane.showMessageDialog(null, "El consecutivo de la orden no existe");
            }
            else
            {
                
                
                
                Connection cn = BLOrden.obtenerConexion();
                Map parametros = new HashMap();
                
                parametros.put("NumeroOrden",numOrden );
                
                
                jr=(JasperReport) JRLoader.loadObjectFromFile(archivo);
                JasperPrint jp= JasperFillManager.fillReport(jr, parametros,cn );
                JasperViewer jv= new JasperViewer(jp);
                jv.setDefaultCloseOperation(DISPOSE_ON_CLOSE); 
                jv.setTitle("Orden De Viaticos");
                jv.setVisible(true);
               
                
                
            }
        } catch (SQLException | ClassNotFoundException | JRException ex) {
            Logger.getLogger(FormViaticos.class.getName()).log(Level.SEVERE, null, ex);
        }
       
    }//GEN-LAST:event_jMenuItem5ActionPerformed

    private void BtnLiquidacionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnLiquidacionActionPerformed
        try {
            // TODO add your handling code here:

            VentanaViaticos = new FrmOrdenDeViaticos(BLOrden.ObtenerConsecutivo());
            VentanaViaticos.setVisible(true);
        } catch (SQLException ex) {
            Logger.getLogger(FormViaticos.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_BtnLiquidacionActionPerformed

    private void jmUsuariosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmUsuariosActionPerformed
        FrmManteUsuarios v= new FrmManteUsuarios();
        v.setVisible(true);
        
    }//GEN-LAST:event_jmUsuariosActionPerformed


    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FormViaticos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FormViaticos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FormViaticos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FormViaticos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FormViaticos().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BtnLiquidacion;
    private javax.swing.JMenu MnuConceptos;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenu jMenu5;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuBar jMenuBar2;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jmUsuarios;
    private javax.swing.JMenuItem menuAnular;
    private javax.swing.JMenuItem mnNuevoViatico;
    // End of variables declaration//GEN-END:variables
}
